package com.kafka.task;

import com.kafka.service.ApiService;
import com.kafka.service.ApiService_Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author syd
 * @create 2020-12-25 10:07
 */
@Component
public class CronTask {

    private static Logger logger = LoggerFactory.getLogger(CronTask.class);

    @Autowired
    private ApiService apiService;
    private static int sta_value=1;
    private static int dyn_value=1;
    private static int water_value=1;

    @Value("${filepath}")
    private String filepath;


    File sta_file;
    File dyn_file;
    File water_file;


    //航标静态
    @Scheduled(fixedRate = 60000)
    private void getAidsSta() throws InterruptedException, IOException {

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        if(sta_value==1){
            logger.info("************获取航标静态信息**********");
            String s = apiService.getAllBuoyInfo();
            sta_value++;

            logger.info("***航标静态信息写入文件***");

            sta_file = new File(filepath+"aids_sta.source");

            if(!sta_file.exists()){
                sta_file.createNewFile();
            }

            //write overwrite file
            FileWriter fileWriter = new FileWriter(sta_file, false);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(s);
            //bufferedWriter.flush();
            bufferedWriter.close();
            //fileWriter.flush();
            fileWriter.close();
        }

    }

    //航标动态
    @Scheduled(fixedRate = 60000)
    private void getAidsDyn() throws InterruptedException, IOException {

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        logger.info("************获取航标动态信息**********");
        String s = apiService.getAllBuoyDyn()+"\n";
        logger.info("***航标动态信息写入文件***");

        if(dyn_value==1) {

            dyn_file = new File(filepath + "aids_dyn.source");
            if(dyn_file.exists()){
                dyn_file.delete();
                dyn_file.createNewFile();
            }
            dyn_value++;
        }
        //write append file
        FileWriter fileWriter = new FileWriter(dyn_file, true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(s);
        //bufferedWriter.flush();
        bufferedWriter.close();
        //fileWriter.flush();
        fileWriter.close();
    }

    //水位动态
    @Scheduled(fixedRate = 60000)
    private void getBuoySta() throws InterruptedException, IOException {

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        logger.info("************获取水位动态信息**********");
        String s = apiService.getAllWaterStationDyn()+"\n";
        logger.info("***水位动态信息写入文件***");
        if(water_value==1){
            water_file= new File(filepath+"aids_water.source");
            if(water_file.exists()){
                water_file.delete();
                water_file.createNewFile();
            }
            water_value++;
        }

        //write append file
        FileWriter fileWriter = new FileWriter(water_file, true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(s);
        //bufferedWriter.flush();
        bufferedWriter.close();
        //fileWriter.flush();
        fileWriter.close();

    }

}